package com.daxia.bumall.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.daxia.bumall.dto.JsonResultDTO;
import com.daxia.bumall.dto.OrderItemDTO;
import com.daxia.bumall.service.OrderItemService;
import com.daxia.bumall.support.Page;
import com.daxia.bumall.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/orderItem", produces="text/html;charset=UTF-8")
public class AdminOrderItemController extends BaseController {

	@Autowired
	private OrderItemService orderItemService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		if (id != null) {
			OrderItemDTO dto = orderItemService.load(id);
			map.put("n", dto);
		}
		return "admin/orderItem/orderItem_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	
	public String save(OrderItemDTO dto, JsonResultDTO resultDTO) throws Exception {
		if (dto.getId() == null) {
			orderItemService.create(dto);
		} else {
			orderItemService.updateAllFields(dto);
		}
		return okAndClose(resultDTO);
	}

    
	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids, JsonResultDTO resultDTO) throws IOException {
		orderItemService.deleteByIds(ids);
		return okAndRefresh(resultDTO);
	}
	
	@RequestMapping(value = "/list")
	
	public String list(OrderItemDTO dto, Map<String, Object> map, Page page) {
		List<OrderItemDTO> dtos = orderItemService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		return "admin/orderItem/orderItem_list";
	}
	
	@ResponseBody
	@RequestMapping(value = "/datagrid")
    
    public String datagrid(OrderItemDTO dto, Map<String, Object> map, Page page) throws Exception {
        List<OrderItemDTO> dtos = orderItemService.find(dto, page);
        JSONArray jsonArray = new JSONArray();
        for (OrderItemDTO orderItem : dtos) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", orderItem.getId());
	        jsonObject.put("order.orderNo", orderItem.getOrder().getOrderNo());    	
	        jsonObject.put("product.name", orderItem.getProduct().getName());    	
	        //jsonObject.put("specification.name", orderItem.getSpecification().getName());    	
            jsonObject.put("sellingPrice", orderItem.getSellingPrice());
            jsonObject.put("payPrice", orderItem.getPayPrice());
            jsonObject.put("quantity", orderItem.getQuantity());
            jsonArray.add(jsonObject);
        }
        JSONObject json = new JSONObject();
        json.put("total", page.getTotalRecords());
        json.put("rows", jsonArray);
        return json.toJSONString();
    }
}
